package com.activity.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.activity.fragment.BottomMenuActivity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.login.R;
import com.tools.Address;
import com.tools.OkHttp3;
import com.tools.goods.GoodsAdapter;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {
    private Button register;
    private ImageView back, pwdVis;
    private EditText account, firstPwd, secPwd;
    private TextView pwdTip;
    private Boolean isSame = false;
    private Boolean isVis = false;
    private JSONObject resJSON;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x001) {
                if (resJSON.getString("code").charAt(0) == '2'){
                    String data = resJSON.getString("data");
                    Toast.makeText(RegisterActivity.this, data + "，请登录", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    finish();
                    startActivity(intent);
                }else{
                    Toast.makeText(RegisterActivity.this, resJSON.getString("message"), Toast.LENGTH_SHORT).show();
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();

        back.setOnClickListener(view -> {
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            finish();
            startActivity(intent);
        });

        secPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!secPwd.getText().toString().equals("")){
                    pwdVis.setVisibility(View.VISIBLE);
                }else{
                    pwdVis.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                confirm(firstPwd, secPwd);
            }
        });

        pwdVis.setOnClickListener(view -> {
            if (!isVis){
                isVis = true;
                pwdVis.setImageResource(R.drawable.pwd_invis);
                secPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                isVis = false;
                pwdVis.setImageResource(R.drawable.pwd_vis);
                secPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                //光标移至文本后面
            }
            secPwd.setSelection(secPwd.getText().toString().length());
        });

        register.setOnClickListener(view -> {
            if (isSame){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", account.getText().toString());
                jsonObject.put("pwd", firstPwd.getText().toString());
                RequestBody body = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), jsonObject.toJSONString());

                OkHttp3.sendOkHttpPostRequest(Address.getUserRegister(), body, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(RegisterActivity.this, "网络连接失败！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String resPost = response.body().string();
                        resJSON = JSON.parseObject(resPost);
                        handler.sendEmptyMessage(0x001);
                    }
                });
            }
        });
    }

    private void init(){
        register = findViewById(R.id.register_btn);
        back = findViewById(R.id.register_back);
        account = findViewById(R.id.register_account);

        firstPwd = findViewById(R.id.register_password);
        firstPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        secPwd = findViewById(R.id.register_password_confirm);
        secPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

        pwdVis = findViewById(R.id.register_pwd_visible);
        pwdTip = findViewById(R.id.register_pwd_tip);
    }

    private void confirm(EditText editA, EditText editB){
        String a = editA.getText().toString();
        String b = editB.getText().toString();
        if (!a.equals(b)){
            pwdTip.setVisibility(View.VISIBLE);
            isSame = false;
        }else {
            pwdTip.setVisibility(View.GONE);
            isSame = true;
        }
    }
}